package com.base.commonlib.widget;

import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.base.commonlib.R;


/**
 * @author weikailiang
 * @function Created on 2017/3/24.
 */

public class PhotoSelectManager {
    private static PhotoSelectManager mInstance;
    private popListener listener;
    private PopupWindow popupWindow;
    private PhotoSelectManager(){}
    public static PhotoSelectManager getmInstance(){
        if (mInstance == null){
            synchronized (PhotoSelectManager.class){
                if (mInstance == null){
                    mInstance = new PhotoSelectManager();
                }
            }
        }
        return mInstance;
    }
    public void show(final Activity mActivity, final popListener listener){
        this.listener = listener;
        final View view = mActivity.getLayoutInflater().inflate(R.layout.item_select_photo_layout,null);
        TextView camera =  view.findViewById(R.id.camera);//从相机获取
        TextView photos =  view.findViewById(R.id.photos);//从相册获取
        TextView cacel =  view.findViewById(R.id.cacle);//取消
        camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.cameraClick((TextView) view);
            }
        });
        photos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.albumClick((TextView) view);
            }
        });
        cacel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                listener.cancelClick((TextView) view);
            }
        });
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT,true);
        popupWindow.setFocusable(true);
        popupWindow.setContentView(view);
        //在PopupWindow里面就加上下面代码，让键盘弹出时，不会挡住pop窗口。
        popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(mActivity.getResources().getDrawable(
                R.drawable.bg_pop));
        popupWindow.showAtLocation(view, Gravity.AXIS_X_SHIFT | Gravity.BOTTOM,0,0);
        setWindowBg(0.5f,mActivity);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setWindowBg(1.0f,mActivity);
            }
        });
    }
    public void setOnDismissListener(PopupWindow.OnDismissListener listener){
        popupWindow.setOnDismissListener(listener);
    }
   public void onDestroy(){
       if (listener != null){
           listener = null;
       }
   }
    /**
     * 窗体透明度变化
     */
    private void setWindowBg(float bgAlpha,Activity activity){
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        activity.getWindow().setAttributes(lp);
    }
    public void dismiss(){
        if (popupWindow.isShowing()){
            popupWindow.dismiss();
        }
    }
    /**
     * pop窗体监听接口
     */
    public interface popListener{
        void cameraClick(TextView camera);
        void albumClick(TextView photos);
        void cancelClick(TextView cancel);
    }
}
